﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Deep_Talk_Converter.Controls
{
    /// <summary>
    /// A text box that will auto-select like the IE address bar when you first put your mouse in it.
    /// </summary>
    public class TextBoxAutoSelect : TextBox
    {
        protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            SelectAll();
        }

        protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
        {
            base.OnGotKeyboardFocus(e);
            SelectAll();
        }

        /// <summary>
        /// Prevent dealing with the left mouse button if this is the first
        /// click. Otherwise we need to deal with it normally so the caret can
        /// be normally positioned.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            if (!IsKeyboardFocusWithin)
            {
                e.Handled = true;
                Focus();
            }
            else
            {
                base.OnPreviewMouseLeftButtonDown(e);
            }
        }
    }
}
